<?php
namespace Admin\Controller;
use Think\Controller;
//Auth认证管理
class AuthController extends AdminCommonController {
	
	//用户列表
	public function index() {
		//获取用户信息
		$user = D("Admin") -> relation(true) -> field("admin_pwd", true) -> select();
		$this -> user = $user;
		//获取用户组信息
		$group = M("auth_group") -> select();
		$obj = M("auth_rule");
		foreach ($group as $k => $v) {
			$map['id'] = array('in', $group[$k]['rules']);
			$group[$k]['group'] = $obj -> where($map) -> select();
		}
		$this -> group = $group;
		//获取rule规则
		$this -> rule = M("auth_rule") -> select();
		//$ip = new \Org\Net\IpLocation("UTFWry.dat");
		//$location=$ip->getlocation();
		//p($location);die;
		//p($group);die;
		$this -> display();
	}

	//添加用户页面
	public function addUser(){
		$group = M("auth_group") -> select();
		$this -> group = $group;
		$this -> display();
	} 
	//添加后台用户及表单处理
	public function user() {
		if (IS_POST) {
			$data = array(
				'admin_name' => I('admin_name', '', 'trim'), 
				'nickname' => I('nickname', '', 'trim'), 
				'admin_pwd' => I('admin_pwd', '', 'md5'), 
				'status' => I('status', 0, 'intval'), 
				'create_time' => $_SERVER['REQUEST_TIME']
			);
			if (empty($_POST['role'])) {
				$this -> error("请选择用户组...");
			}
			if ($data['admin_pwd'] != md5($_POST['repwd'])) {
				$this -> error("两次密码不一致...");
			}
			if ( M("Admin") -> where(array('admin_name' => $data['admin_name'])) -> find()) {
				$this -> error("用户名已存在...");
			}
			$data['role'] = $_POST['role'];
			if ($lastInsertId = M("Admin") -> add($data)) {
//				foreach ($_POST['role'] as $k => $v) { [$k]
					$arr = array('uid' => $lastInsertId, 'group_id' => $_POST['role']);
					M("auth_group_access") -> add($arr);
//				}
				M("auth_group_access") -> add($arr);
				$this -> success("添加成功...", U("Auth/index"));
			} else {
				$this -> error("添加失败...");
			}
		} else {
			$this -> group = M("auth_group") -> field("id,title") -> select();
			$this -> display();
		}

	}
	//用户编辑页面
	public function editUser(){
		if (IS_GET) {
			if (!isset($_GET['id'])) {
				return false;
			}
			$id = I("id", 0, "intval");
			if (!$id) {
				return false;
			}
			if ($user = M("admin") -> find($id)) {
				$this -> data = $user;
				$group = M("auth_group") -> select();
				$this -> group = $group;
				$this -> display();
			} else {
				$this -> error("未找到用户");
			}
		}
	}
		//修改用户信息提交至此
	public function updateUser() {
		if (IS_POST) {
			$id = I("id", 0, "intval");
			if (!$id) {
				return false;
			}
			$data['admin_name'] = I("admin_name", "", "trim");
			$data['nickname'] = I("nickname", "", "trim");
			$data['status'] = I("status", 0, "intval");
			$tmp = 0;
			if (!empty($_POST['role'])) {
				M("auth_group_access") -> where("uid=%d",$id) -> delete();
//				foreach ($_POST['role'] as $key => $value) {  [$key]
					if ( M("auth_group_access") -> add(array("uid" => $id, "group_id" => $_POST['role']))) {
						$tmp = 1;
					}
//				}
				$data['role'] = $_POST['role'];
			} else {
				$this -> error('请选择用户组');
			}
			if (trim($_POST['admin_pwd'])) {
				if (md5($_POST['admin_pwd']) != md5($_POST['repwd'])) {
					$this -> error("两次密码输入不一致");
				} else {
					$data['admin_pwd'] = I("admin_pwd", "", "md5");
				}
			}
			if ( M("Admin") -> where("id=%d",$id) -> save($data)) {
				$this -> success("修改成功", U("index"));
			} else {
				if ($tmp) {
					$this -> success("修改成功", U("index"));
				} else {
					$this -> error("修改失败");
				}
			}
		} elseif (IS_GET) {
			if (!isset($_GET['id'])) {
				return false;
			}
			$id = I("id", 0, "intval");
			if (!$id) {
				return false;
			}
			$this -> user = M("Admin") -> where(array('id' => $id)) -> field("admin_pwd", true) -> find();
			$user_group = M("auth_group_access") -> where(array("id" => $id)) -> select();
			$group = M("auth_group") -> select();
			foreach ($user_group as $key => $value) {
				$user_group[$key]['group_name'] = M("auth_group") -> where(array("id" => $user_group[$key]['group_id'])) -> getField("title");
			}
			$this -> user_group = $user_group;
			$this -> group = $group;
			$this -> display();
		}
	}

	//删除用户
	public function deleteUser() {
		if (IS_GET) {
			if (!isset($_GET['id'])) {
				return false;
			}
			$id = I("id", 0, "intval");
			if (!$id) {
				return false;
			}
			if ( M("admin") -> where(array("id" => $id)) -> delete()) {
				M("auth_group_access") -> where(array("uid" => $id)) -> delete();
				$this -> success("删除成功", U("indexUser"));
			} else {
				$this -> error("删除失败");
			}
		}
	}
	//用户组列表
	public function indexGroup(){
		$group = M("auth_group") -> select();
		$this -> group = $group;
		$this -> display();
	}
	//添加后台用户组及表单处理
	public function group() {
		if (IS_POST) {
			$data = array('title' => I('title', '', 'trim'), 'status' => I('status', 0, 'intval'));
			if ( M("auth_group") -> where(array('title' => $data['title'])) -> find()) {
				$this -> error("用户组名称已存在");
			}
			if ( M("auth_group") -> add($data)) {
				$this -> success("添加成功", U("indexGroup"));
			} else {
				$this -> error("添加失败");
			}
		} else {
			$this -> display();
		}
	}
	
	//编辑用户组页面
	public function editGroup(){
		if (IS_GET) {
			if (!isset($_GET['id'])) {
				return false;
			}
			$id = I("id", 0, "intval");
			if (!$id) {
				return false;
			}
			if ($data = M("auth_group") -> find($id)) {
				$data["rules"] = explode(',',$data['rules']);
				$this -> data = $data;
				$ctrs = M('auth_controller') ->where('status = 1') -> select();
				
				$rule = M("auth_rule")  -> select();
				$rules;
				foreach($ctrs as $c){
					$cr = array();
					foreach ($rule as $r) {
						if($r['type'] == $c['id']){
							$cr[] = $r;
						}
					}
					$rules[] = array('controller' => $c, 'rules' => $cr);
				}
//				dump($rules);
				$this -> list = $rules;
				$this -> display();
			} else {
				$this -> error("未找到用户组");
			}
		}
	}
	//修改用户组信息提交至此
	public function updateGroup() {
		if (IS_GET) {
			if (!isset($_GET['id'])) {
				return false;
			}
			$id = I("id", 0, "intval");
			if (!$id) {
				return false;
			}
			$group = M("auth_group") -> where(array("id" => $id)) -> find();
			$rule = M("auth_rule") -> select();
			foreach ($rule as $k => $v) {
				if (in_array($rule[$k]['id'], explode(',', $group['rules']))) {
					$rule[$k]['is_checked'] = 1;
				} else {
					$rule[$k]['is_checked'] = 0;
				}
			}
			$this -> rule = $rule;
			$this -> group = $group;
			$this -> id = $id;
			//p($rule);die;
			$this -> display();
		} elseif (IS_POST) {
//			dump($_POST['rule']);
			$data = array('title' => I("title", "", "trim"), 'rules' => implode(",", $_POST['rule']), 'status' => I("status", "", "trim"));
			if ( M("auth_group") -> where(array("id" => $_POST['id'])) -> save($data)) {
				$this -> success("修改成功", U("indexGroup"));
			} else {
				$this -> error("修改失败");
			}
		}
	}
	//删除用户组
	public function deleteGroup() {
		if (IS_GET) {
			if (!isset($_GET['id'])) {
				return false;
			}
			$id = I("id", 0, "intval");
			if (!$id) {
				return false;
			}
			if ( M("auth_group") -> where(array("id" => $id)) -> delete()) {
				M("auth_group_access") -> where(array("group_id" => $id)) -> delete();
				$this -> success("删除成功...", U("indexGroup"));
			} else {
				$this -> error("删除失败...");
			}
		}
	}
	//rule列表
	public function indexRule(){
		$rule = M("auth_rule") -> order('name') -> select();
		$this -> rule = $rule;
		$this -> display();
	}
	//添加rule页面
	public function addRule(){
		$list = M('auth_controller') -> where('status=1')-> select();
		$this -> list = $list;
		$this -> display();
	}
	//添加后台权限及表单处理
	public function rule() {
		if (IS_POST) {
			$data = array(
				'name' => I('name', '', 'trim'), 
				'title' => I('title', '', 'trim'), 
				'condition' => I('condition', '', 'trim'), 
				'status' => I('status', 0, 'intval'), 
				'type' => I('type', 0, 'intval'), 
			);
			if ( M("auth_rule") -> add($data)) {
				$this -> success("添加成功...", U("indexRule"));
			} else {
				$this -> error("添加失败...");
			}
		} else {
			$this -> display();
		}
	}
	//rule编辑
	public function editRule(){
		if (IS_GET) {
			if (!isset($_GET['id'])) {
				return false;
			}
			$id = I("id", 0, "intval");
			if (!$id) {
				return false;
			}
			if ($data = M("auth_rule") -> find($id)) {
				$this -> data = $data;
				$this -> display();
			} else {
				$this -> error("未找到该权限");
			}
		}
	}
	

	//删除RULE
	public function deleteRule() {
		if (IS_GET) {
			if (!isset($_GET['id'])) {
				return false;
			}
			$id = I("id", 0, "intval");
			if (!$id) {
				return false;
			}
			if ( M("auth_rule") -> delete($id)) {
				$this -> success("删除成功", U("indexRule"));
			} else {
				$this -> error("删除失败");
			}
		}
	}

	//修改RULE
	public function updateRule() {
		if (IS_POST) {
			if (!isset($_POST['id'])) {
				return false;
			}
			$id = I("id", 0, "intval");
			unset($_POST['id']);
			if (!$id) {
				return false;
			}
			if ( M("auth_rule") -> where(array("id" => $id)) -> save($_POST)) {
				$this -> success("修改成功", U("indexRule"));
			} else {
				$this -> error("修改失败");
			}
		}
	}

	//控制器列表
	public function indexC(){
		$dir=APP_PATH.'Admin/Controller/';
		$file=scandir($dir);
		$C;
		foreach($file as $f){
			if(strstr($f,'php')){
				$arr = explode('Controller',$f);
				if($arr[0]!='AdminCommon'){
					$C[] = $arr[0];
				}
			}
		}
		$ct = M('auth_controller');
		foreach($C as $c){
			$data['name'] = $c;
			if(!($ct -> where(array('name' => $data['name'])) -> find())){
				
				$ct -> add($data);
				
			}
		}
		$list = $ct -> select();
		$this -> list = $list;
		$this -> display();
	}
	//修改控制器信息
	public function editC(){
		if (IS_GET) {
			if (!isset($_GET['id'])) {
				return false;
			}
			$id = I("id", 0, "intval");
			if (!$id) {
				return false;
			}
			if ($user = M("auth_controller") -> find($id)) {
				$this -> data = $user;
				$this -> display();
			} else {
				$this -> error("未找到控制器信息");
			}
		}
	}
	//更新控制器信息
	public function updateC(){
		if (IS_POST) {
			if (!isset($_POST['id'])) {
				return false;
			}
			$id = I("id", 0, "intval");
			unset($_POST['id']);
			if (!$id) {
				return false;
			}
			if ( M("auth_controller") -> where(array("id" => $id)) -> save($_POST)) {
				$this -> success("修改成功", U("indexC"));
			} else {
				$this -> error("修改失败");
			}
		}
	}
	//通用改变状态方法
	public function updateStatus(){
		$id = I("get.id");
		$m = I('get.m');
        $Model = M($m);
        $Model -> find($id);
        if($Model -> status == 0){
            $Model -> status = 1;
        } else {
            $Model -> status = 0;
        }
        $Model -> save();
        $this -> success("状态修改成功");
	}
	//空操作，用于无需加载数据而只需要渲染模板的时候
	public function _empty($name){
	 	$this -> display($name);
	}
}
